﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VNPT.DoiSoat.DoiSoat;

namespace VNPT.DoiSoat.BaoCao
{
    public partial class uBC_1088 : Core.uBase
    {
        public uBC_1088()
        {
            InitializeComponent();
        }
        int thang = DateTime.Now.Month;
        int nam = DateTime.Now.Year;

        List<Data.BaoCao.BC_SanLuong1088> li = new List<Data.BaoCao.BC_SanLuong1088>();

        protected override void OnLoad(EventArgs e)
        {
            thang = DoiSoatSetting.THANG;
            nam = DoiSoatSetting.NAM;
            base.OnLoad(e);
        }
        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {

        }

        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            li.Clear();
            if (!string.IsNullOrWhiteSpace(lookUpEdit1.Text) && !string.IsNullOrWhiteSpace(btnMaMang.Text))
            {
                using (var dlg = new DevExpress.Utils.WaitDialogForm("Đang tải template..."))
                {
                    var data = VNPT.Utility.ExcelHelper.GetData(btnMaMang.Text, lookUpEdit1.Text);
                    foreach (var item in data.Rows)
                    {
                        var dauso = (((DataRow)item)["DAUSO"] ?? "").ToString();
                        var dichvu = (((DataRow)item)["DICHVU"] ?? "").ToString();
                        if (!string.IsNullOrWhiteSpace(dauso) && !string.IsNullOrWhiteSpace(dichvu))
                        {
                            li.Add(new Data.BaoCao.BC_SanLuong1088()
                            {
                                Cuoc_CD = 0,
                                Cuoc_DD = 0,
                                Nhom = dichvu,
                                Phut_CD = 0,
                                Phut_DD = 0,
                                SoDT = dauso
                            });
                        }
                        else
                        {
                            continue;
                        }
                    }
                    bC_SanLuong1088BindingSource.DataSource = li;
                    var sql = string.Format(DoiSoatSetting.SQL_BAOCAO_1088, nam, thang);
                    // MessageBox.Show(sql);
                    var dt_sv = DbServer.GetDataTable(new Data.QueryCommandConfig(sql));
                    foreach (var item in dt_sv.Rows)
                    {
                        var ro = (DataRow)item;

                        var bgoi = (ro["BIGOI"] ?? "").ToString();
                        var loai = (ro["LOAI"] ?? "").ToString();

                        int _out = 0;
                        int.TryParse((ro["PHUT"] ?? "").ToString(), out _out);
                        var phut = _out;

                        _out = 0;
                        int.TryParse((ro["CUOC"] ?? "").ToString(), out _out);
                        var cuoc = _out;

                        var m = li.Where(p => p.SoDT == bgoi);
                        if (m.Any())
                        {
                            var m2 = m.First();
                            if (loai == "CD")
                            {
                                m2.Phut_CD += phut;
                                m2.Cuoc_CD += cuoc;
                            }
                            else
                            {
                                m2.Phut_DD += phut;
                                m2.Cuoc_DD += cuoc;
                            }
                        }
                    }
                }
                myGridControl.RefreshDataSource();
            }
            else
            {
                MessageBox.Show("Chưa chọn đủ dữ liệu! Vui long kiểm tra lại");
            }

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            new VNPT.Report.Reporter().Print_SanLuong_1088(bC_SanLuong1088BindingSource, thang, nam);
        }
        private void btnMaMang_EditValueChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(this.btnMaMang.Text))
            {
                lookUpEdit1.Properties.DataSource = VNPT.Utility.ExcelHelper.GetSheetName(btnMaMang.Text);
            }
        }

        private void btnSQL_Click(object sender, EventArgs e)
        {
            new fEditSQL("SQL_BAOCAO_1088",
             "Lấy số liệu gọi vào các đầu số theo danh sách",
             "Các tham số {0}= Năm, {1:D2}= Tháng").ShowDialog();
        }
    }
}
